<template>
  <el-form
    ref="loginForm"
    :model="form"
    :rules="rules"
    @submit.prevent="handleSubmit"
    class="login-form"
  >
    <el-form-item prop="username">
      <el-input
        v-model="form.username"
        placeholder="请输入用户名"
        size="large"
        :prefix-icon="User"
      />
    </el-form-item>
    
    <el-form-item prop="password">
      <el-input
        v-model="form.password"
        type="password"
        placeholder="请输入密码"
        size="large"
        :prefix-icon="Lock"
        show-password
      />
    </el-form-item>
    
    <el-form-item>
      <el-button
        type="primary"
        native-type="submit"
        :loading="loading"
        size="large"
        class="submit-btn"
      >
        <el-icon v-if="!loading"><Right /></el-icon>
        {{ loading ? '登录中...' : '登录' }}
      </el-button>
    </el-form-item>
    
    <div class="form-options">
      <el-checkbox v-model="rememberMe">记住我</el-checkbox>
      <el-link type="primary" class="forgot-password">忘记密码？</el-link>
    </div>
  </el-form>
</template>

<script>
import { ref, reactive } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, Right } from '@element-plus/icons-vue'

export default {
  name: 'LoginForm',
  components: {
    User,
    Lock,
    Right
  },
  setup() {
    const store = useStore()
    const router = useRouter()
    const loginForm = ref()
    const loading = ref(false)
    const rememberMe = ref(false)
    
    const form = reactive({
      username: '',
      password: ''
    })
    
    const rules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    const handleSubmit = async () => {
      if (!loginForm.value) return
      
      try {
        await loginForm.value.validate()
        loading.value = true
        
        // 模拟登录API调用
        setTimeout(() => {
          const mockUser = {
            id: 1,
            username: form.username,
            email: 'user@example.com',
            role: 'admin'
          }
          const mockToken = 'mock-jwt-token'
          
          store.dispatch('login', { token: mockToken, user: mockUser })
          ElMessage.success('登录成功')
          router.push('/dashboard')
          loading.value = false
        }, 1000)
        
      } catch (error) {
        console.error('登录错误:', error)
        ElMessage.error('登录失败')
        loading.value = false
      }
    }
    
    return {
      loginForm,
      form,
      rules,
      loading,
      rememberMe,
      handleSubmit,
      User,
      Lock,
      Right
    }
  }
}
</script>

<style scoped>
.login-form {
  width: 100%;
}

.login-form .el-form-item {
  margin-bottom: 25px;
}

.submit-btn {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 25px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(102, 126, 234, 0.3);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 20px;
}

.forgot-password {
  font-size: 14px;
}

.el-input {
  border-radius: 10px;
}

.el-input__wrapper {
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.el-input__wrapper:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.el-input__wrapper.is-focus {
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}
</style> 